﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarbonExchange.Bll
{
    public class EmailRestriction : ItemBase
    {
        public EmailRestriction (int siteId)
        {
            this.SiteId = siteId;
        }

        internal EmailRestriction()
        {
        }

        public int Id { get; internal set; }
        public int SiteId { get; internal set; }
        public string EmailFilter { get; set; }
        public bool Access { get; set; }
        public DateTime Created { get; internal set; }

        public enum ValidationResult
        {
            Allowed,
            Refused,
            NotFound
        }
        public ValidationResult ValidateEmail(string emailAddress)
        {
            bool match = false;
            var tmpEmailFilter = EmailFilter;
            if (!EmailFilter.Contains("[") && !EmailFilter.Contains("]") && !EmailFilter.Contains("{") && !EmailFilter.Contains("}"))
            {
                //if (EmailFilter.StartsWith("*") && EmailFilter.LastIndexOf("*") == 0)
                //{
                    //turn *@email.com into .{1,}@email.com{1}$
                //    tmpEmailFilter = string.Format(".{{1,}}{0}{{1}}$", EmailFilter.Substring(1).Replace(".", "\\."));
                //}
                //else if (EmailFilter.EndsWith("*") && EmailFilter.IndexOf("*") == EmailFilter.Length - 1)
                //{
                //    //turn this@email.* into ^this@email.{1}.{1,}$
                //    tmpEmailFilter = string.Format("^{0}{{1}}.{{1,}}", EmailFilter.Substring(0, EmailFilter.Length - 1).Replace(".", "\\."));
                //}
                //else 
                //if (EmailFilter.Contains("*"))
                //{
                    //turn blar*@email.com into ^blar{1}.{1,}@email.com{1}$
                var tmpParts = EmailFilter.Split('*');
                /*StringBuilder tmp = new StringBuilder();
                    tmp.Append("^");
                    var spacer = "";
                    for(int i = 0; i <tmpParts.Length; i++)
                    {
                        tmp.Append(spacer);
                        tmp.Append(tmpParts[i].Replace(".", "\\."));
                        spacer = ".{1,}";
                    }
                    tmp.Append("$");*/
                    tmpEmailFilter = tmpParts.AsQueryable().ToFormatedStringWithSpacer(".{1,}", x => x.Replace(".", "\\."));
                    tmpEmailFilter = string.Format("^{0}$", tmpEmailFilter);
                //}
                //else
                //{
                    //turn @email.com into ^@email.com$
//                    tmpEmailFilter = string.Format("^{0}$", EmailFilter.Replace(".", "\\."));
                //}
            }
            
            try
            {
                match = System.Text.RegularExpressions.Regex.Match(emailAddress, tmpEmailFilter, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success;
            }
            catch
            {
                //if failed try escaping the filter string
                string emailFilterEscaped = System.Text.RegularExpressions.Regex.Escape(EmailFilter);
                match = System.Text.RegularExpressions.Regex.Match(emailAddress, emailFilterEscaped, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success; 
            }

            if (match)
            {
                return Access ? ValidationResult.Allowed : ValidationResult.Refused;
            }
            else
            {
                return ValidationResult.NotFound;
            }
        }
    }
}
